﻿// -----------------------------------------------------------------------
// <copyright file="AttachmentWorkOrder_DAL.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace PMSGSC.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class AttachmentWorkOrder_DAL:BaseDAL
    {
        // Lấy tất cả AttachmentWorkOrder
        public static List<AttachmentWorkOrder> GetAll(int shipID)
        {
            DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.AttachmentWorkOrders);
            return DB.AttachmentWorkOrders.Where(n => n.WorkOrder.Equipment.Ship_ID == shipID).ToList();
        }
        // Lấy AttachmentWorkOrder theo Id
        public static AttachmentWorkOrder GetDataById(int id)
        {
            return DB.AttachmentWorkOrders.Where(m => m.Attachment_ID == id).FirstOrDefault();
        }
        //Thêm một AttachmentWorkOrder
        public static bool Insert(AttachmentWorkOrder AttachmentWorkOrder)
        {
            try
            {
                DB.AttachmentWorkOrders.InsertOnSubmit(AttachmentWorkOrder);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        // Xóa một AttachmentWorkOrder
        public static bool Delete(AttachmentWorkOrder AttachmentWorkOrder)
        {
            try
            {
                DB.AttachmentWorkOrders.DeleteOnSubmit(AttachmentWorkOrder);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        public static AttachmentWorkOrder GetDataByAttachmentJob(int id)
        {
            return DB.AttachmentWorkOrders.Where(n => n.AttachmentJob_ID == id).FirstOrDefault();
        }
        public static bool DeleteByID(int id)
        {
            try
            {
                AttachmentWorkOrder aw = GetDataByAttachmentJob(id);
                DB.AttachmentWorkOrders.DeleteOnSubmit(aw);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        // Cập nhật AttachmentWorkOrder
        public static bool Update(AttachmentWorkOrder AttachmentWorkOrder)
        {
            try
            {
                //AttachmentWorkOrder objAttachment = GetDataById(AttachmentWorkOrder.Attachment_ID);
                //objAttachment.Code = AttachmentWorkOrder.Code;
                //objAttachment.Job_ID = AttachmentWorkOrder.Job_ID;
                //objAttachment.ContentFile = AttachmentWorkOrder.ContentFile;
                //objAttachment.Name = AttachmentWorkOrder.Name;
                //objAttachment.Extentsion = AttachmentWorkOrder.Extentsion;
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        // Kiểm tra xem AttachmentWorkOrder Code có tồn tại chưa ?
        public static bool IsExits(AttachmentWorkOrder AttachmentWorkOrder)
        {
            AttachmentWorkOrder objAttachmentWorkOrder = null;
            objAttachmentWorkOrder = DB.AttachmentWorkOrders.Where(m => (m.Code == AttachmentWorkOrder.Code) &&
                (m.Attachment_ID != AttachmentWorkOrder.Attachment_ID) && (m.WorkOrder_ID == AttachmentWorkOrder.WorkOrder_ID)).FirstOrDefault();

            return objAttachmentWorkOrder != null ? true : false;
        }

        public static bool IsExitsID(AttachmentWorkOrder AttachmentWorkOrder)
        {
            AttachmentWorkOrder objAttachmentWorkOrder = null;
            objAttachmentWorkOrder = DB.AttachmentWorkOrders.Where(m => (m.Attachment_ID == AttachmentWorkOrder.Attachment_ID) && (m.WorkOrder_ID == AttachmentWorkOrder.WorkOrder_ID)).FirstOrDefault();
            return objAttachmentWorkOrder != null ? true : false;
        }

        public static List<AttachmentWorkOrder> GetDataByworkOrderID(int id, bool isChanged)
        {
            if (isChanged)
            {
                DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.AttachmentWorkOrders);
            }
            return DB.AttachmentWorkOrders.Where(j => j.WorkOrder_ID == id).ToList();
        }

    }
}
